package net.homeftp.heimo.weddingwidget;

import java.util.Calendar;
import java.util.Date;

import net.homeftp.heimo.weddingwidget.perf.MyPreferenceManager;
import net.homeftp.heimo.weddingwidget.ui.colorpicker.ColorPickerDialog;
import net.homeftp.heimo.weddingwidget.ui.colorpicker.OnColorPickedListener;
import net.homeftp.heimo.weddingwidget.ui.imgdialog.ImageListAdapter;
import net.homeftp.heimo.weddingwidget.ui.imgdialog.ImageListDialog;
import net.homeftp.heimo.weddingwidget.ui.imgdialog.OnImgListener;
import net.homeftp.heimo.weddingwidget.ui.seekbar.OnSeekListener;
import net.homeftp.heimo.weddingwidget.ui.seekbar.SeekBarDialog;
import net.homeftp.heimo.weddingwidget.ui.textdialog.OnTextListener;
import net.homeftp.heimo.weddingwidget.ui.textdialog.TextDialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class WeddingPreferenceControlActivity extends Activity implements
		OnClickListener, OnGestureListener {
	private static final String TAG = "WeddingPreferenceControlActivity";
	private MyPreferenceManager prefMgr;
	private WeddingWidgetHelper helper;
	private Context ctx = null;
	private TextView prefBasicAnni, prefBasicCounterType, prefCounterPattern,
			prefLayoutTrans, prefBtext, prefTtext, prefTtextSize,
			prefBtextSize;
	private ImageView prefCounterBg, prefCounterFontColor, prefCounterTheme,
			prefTtextBG, prefTtextColor, prefBtextBG, prefBtextColor,
			prefBasicLayout;
	private View prefPreview;
	private GestureDetector detector;

	private static final int MAX_FONT_SIZE = 10;
	private static final int MAX_FONT_TRANSPARENCY = 100;

	// OnFing settings
	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 100;

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ctx = this;
		setTheme(android.R.style.Theme_Translucent);
		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.main);
		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.ic_android_yan);
		fnInitControls();
		fnInitControlsSettings();
		detector = new GestureDetector(this);

		fnUpdateActivity();
	}

	private void fnUpdateActivity() {
		Intent i = getIntent();
		Bundle extras = i.getExtras();
		if (extras != null) {
			int appWidgetId = extras.getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			prefMgr = new MyPreferenceManager(this, appWidgetId);
			fnUpdateTitle(appWidgetId);
			Log.i(TAG, "[fnUpdateActivity] appWidgetId : " + appWidgetId);
		} else {
			prefMgr = new MyPreferenceManager(this);
			fnUpdateTitle(-1);
			Log.i(TAG, "[fnUpdateActivity]");
		}
		helper = new WeddingWidgetHelper(this, prefMgr);
		fnLoadDefaultSettings();
		fnUpdatePreview();
	}

	private void fnUpdateTitle(int appWidgetId) {
		TextView prefBasicTitle = (TextView) findViewById(R.id.pref_layout_basic_title);
		TextView prefCounterTitle = (TextView) findViewById(R.id.pref_layout_counter_title);
		TextView prefTopTitle = (TextView) findViewById(R.id.pref_layout_top_text_title);
		TextView prefBottomTitle = (TextView) findViewById(R.id.pref_layout_bottom_text_title);
		String basicTitle = getString(R.string.pref_setting_basic_title);
		String counterTitle = getString(R.string.pref_setting_basic_title);
		String topTitle = getString(R.string.pref_setting_basic_title);
		String bottomTitle = getString(R.string.pref_setting_basic_title);

		if (appWidgetId == -1) {
			prefBasicTitle.setText(basicTitle + " (Default)");
			prefCounterTitle.setText(counterTitle + " (Default)");
			prefTopTitle.setText(topTitle + " (Default)");
			prefBottomTitle.setText(bottomTitle + " (Default)");
		} else {
			prefBasicTitle.setText(basicTitle + " (Widget : " + appWidgetId
					+ ")");
			prefCounterTitle.setText(counterTitle + " (Widget : " + appWidgetId
					+ ")");
			prefTopTitle.setText(topTitle + " (Widget : " + appWidgetId + ")");
			prefBottomTitle.setText(bottomTitle + " (Widget : " + appWidgetId
					+ ")");
		}
//		if(scale>1){
//		Resources resources = ctx.getResources();
//		float scale = resources.getDisplayMetrics().density;	
//			float size = prefBasicTitle.getTextSize();
//			float newSize = size*scale;
//			
//			prefBasicTitle.setTextSize(newSize);
//			prefCounterTitle.setTextSize(newSize);
//			prefTopTitle.setTextSize(newSize);
//			prefBottomTitle.setTextSize(newSize);
//		}
	}

	private void fnInitControls() {
		prefBasicAnni = (TextView) findViewById(R.id.pref_basic_anni);
		prefBasicCounterType = (TextView) findViewById(R.id.pref_basic_counter_type);
		prefBasicLayout = (ImageView) findViewById(R.id.pref_basic_layout);
		prefLayoutTrans = (TextView) findViewById(R.id.pref_basic_trans);

		prefCounterBg = (ImageView) findViewById(R.id.pref_counter_bg);
		prefCounterFontColor = (ImageView) findViewById(R.id.pref_counter_font_color);
		prefCounterPattern = (TextView) findViewById(R.id.pref_counter_pattern);
		prefCounterTheme = (ImageView) findViewById(R.id.pref_counter_theme);

		prefTtext = (TextView) findViewById(R.id.pref_ttext);
		prefTtextBG = (ImageView) findViewById(R.id.pref_ttext_bg);
		prefTtextColor = (ImageView) findViewById(R.id.pref_ttext_color);
		prefTtextSize = (TextView) findViewById(R.id.pref_ttext_size);

		prefBtext = (TextView) findViewById(R.id.pref_btext);
		prefBtextBG = (ImageView) findViewById(R.id.pref_btext_bg);
		prefBtextColor = (ImageView) findViewById(R.id.pref_btext_color);
		prefBtextSize = (TextView) findViewById(R.id.pref_btext_size);
	}

	private void fnInitControlsSettings() {
		Typeface font = Typeface.createFromAsset(this.getAssets(),
				"fonts/FFF_Tusj.ttf");

		prefBasicAnni.setTypeface(font);
		prefBasicCounterType.setTypeface(font);
		prefLayoutTrans.setTypeface(font);

		prefCounterPattern.setTypeface(font);
		prefTtext.setTypeface(font);
		prefTtextSize.setTypeface(font);
		prefBtext.setTypeface(font);
		prefBtextSize.setTypeface(font);
	}

	private void fnLoadDefaultSettings() {
		// Basic Items
		String prefAnni = prefMgr.getDateFormatter().format(
				prefMgr.getPrefAnniversary());
		fnSetAnniValue(prefAnni);
		prefBasicCounterType.setText(prefMgr.getPrefCounterTypeName());
		fnSetPrefLayout(prefMgr.getPrefLayout());
		fnSetPrefAlpha(prefMgr.getPrefAlpha());
		// Basic Items
		fnSetPrefViewBgColor(prefCounterBg, prefMgr.getPrefCounterBG());
		fnSetPrefViewBgColor(prefCounterFontColor,
				prefMgr.getPrefCounterFontBG());
		prefCounterPattern.setText(prefMgr.getPrefCounterPatternName());
		fnSetCounterTheme(prefMgr.getPrefTheme());
		// Top Text Items
		prefTtext.setText(prefMgr.getPrefTText());
		fnSetPrefViewBgColor(prefTtextBG, prefMgr.getPrefTtextBG());
		fnSetPrefViewBgColor(prefTtextColor, prefMgr.getPrefTtextFontBG());
		prefTtextSize.setText(prefMgr.getPrefTtextSize() + " sp");
		// Bottom Text Items
		prefBtext.setText(prefMgr.getPrefBText());
		fnSetPrefViewBgColor(prefBtextBG, prefMgr.getPrefBtextBG());
		fnSetPrefViewBgColor(prefBtextColor, prefMgr.getPrefBtextFontBG());
		prefBtextSize.setText(prefMgr.getPrefBtextSize() + " sp");
	}

	private void fnSetAnniValue(String prefAnni) {
		prefBasicAnni.setText(prefAnni);
	}

	private void fnSetCounterTheme(int prefTheme) {
		String cntxt = helper.fnGetDayInStringPattern();
		Bitmap bm = helper.buildBitmapUpdate(cntxt, true);
		prefCounterTheme.setImageBitmap(bm);
	}

	private void fnResetPreviewLayout() {
		LinearLayout item = (LinearLayout) findViewById(R.id.preview);
		LayoutInflater lif = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		prefPreview = lif.inflate(R.layout.widget_layout_1o, item);

		String topText = prefMgr.getPrefTText();
		TextView tv = (TextView) prefPreview.findViewById(R.id.top_text);
		if (topText.length() == 0 || topText.equalsIgnoreCase("")) {
			tv.setVisibility(View.GONE);
		} else {
			tv.setVisibility(View.VISIBLE);
		}

		String bottomText = prefMgr.getPrefBText();
		TextView bv = (TextView) prefPreview.findViewById(R.id.bottom_text);
		if (bottomText.length() == 0 || bottomText.equalsIgnoreCase("")) {
			bv.setVisibility(View.GONE);
		} else {
			bv.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}

	private void fnSetPrefAlpha(int alpha) {
		prefLayoutTrans.setText(alpha + "%");
	}

	private void fnSetPrefLayout(int layout) {
		// [0,1,2,3]
		ImageListAdapter ila = new ImageListAdapter(this,
				R.array.img_array_layout);
		int img_id = ila.getImageRefByPos(layout);
		prefBasicLayout.setImageResource(img_id);
	}

	private void fnBtnCounterThemeClicked() {
		OnImgListener imgL = new OnImgListener() {
			@Override
			public void imgValue(int value) {
				prefMgr.setPrefTheme(value);
				fnSetCounterTheme(value);
				fnUpdatePreview();
			}
		};
		ImageListDialog imgD = new ImageListDialog(this, imgL,
				R.array.img_array_theme);
		imgD.setTitle(getString(R.string.title_counter_theme));
		imgD.show();
	}

	private void fnBtnTtextClicked() {
		String tText = prefMgr.getPrefTText();
		OnTextListener mlistener = new OnTextListener() {
			@Override
			public void textValue(String t) {
				prefMgr.setPrefTText(t);
				prefTtext.setText(t);
				fnUpdatePreview();
			}
		};
		TextDialog dlg = new TextDialog(this, mlistener, tText);
		dlg.setTitle(getString(R.string.title_ttext));
		dlg.show();
	}

	private void fnBtnLayoutTransClicked() {
		int alpha = prefMgr.getPrefAlpha();
		OnSeekListener mlistener = new OnSeekListener() {
			@Override
			public void seekValue(int alpha) {
				prefMgr.setPrefAlpha(alpha);
				fnSetPrefAlpha(alpha);
				fnUpdatePreview();
			}

			public void seekProgress(int progress) {
				fnSetPrefAlpha(progress);
			}
		};
		SeekBarDialog sd = new SeekBarDialog(this, mlistener, alpha,
				MAX_FONT_TRANSPARENCY);
		sd.setTitle(R.string.title_basic_trans);
		sd.show();
	}

	private void fnBtnBtextClicked() {
		String bText = prefMgr.getPrefBText();
		OnTextListener mlistener = new OnTextListener() {
			@Override
			public void textValue(String t) {
				prefMgr.setPrefBText(t);
				prefBtext.setText(t);
				fnUpdatePreview();
			}
		};
		TextDialog dlg = new TextDialog(this, mlistener, bText);
		dlg.setTitle(getString(R.string.title_btext));
		dlg.show();
	}

	private void fnSetPrefViewBgColor(ImageView view, int color) {
		// ArcShape, OvalShape, RoundRectShape RectShape
		Drawable d = view.getBackground();
		if (d instanceof GradientDrawable) {
			GradientDrawable draw = (GradientDrawable) view.getBackground();
			draw.setColor(color);
			draw.invalidateSelf();
		}
	}

	private void fnBtnCounterBgClicked() {
		int color = prefMgr.getPrefCounterBG();
		OnColorPickedListener callback = new OnColorPickedListener() {
			@Override
			public void colorPicked(int color) {
				prefMgr.setPrefCounterBG(color);
				fnSetPrefViewBgColor(prefCounterBg, color);
				fnUpdatePreview();
			}
		};

		Dialog cpd = new ColorPickerDialog(this, callback, color, 255);
		cpd.setTitle(R.string.title_counter_bg);
		cpd.show();
	}

	private void fnBtnCounterPatternClicked() {
		final String[] items = getResources().getStringArray(
				R.array.string_array_tagetdt_pattern_name);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Please select a display pattern.");
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				prefCounterPattern.setText(items[item]);
				prefMgr.setPrefCounterPattern(items[item]);
				fnSetCounterTheme(prefMgr.getPrefTheme());
				fnUpdatePreview();
			}
		});
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.title_counter_pattern);
		alert.show();
	}

	private void fnBtnBasicCounterTypeClicked() {
		final String[] items = getResources().getStringArray(
				R.array.string_array_counter_type_name);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Please select a counter type.");
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				prefBasicCounterType.setText(items[item]);
				prefMgr.setPrefCounterType(items[item]);
				fnUpdatePreview();
			}
		});
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.title_counter_type);
		alert.show();
	}

	private void fnBtnBasicAnniversaryClicked() {
		final DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				String setDt = String.valueOf(year) + "."
						+ String.valueOf(monthOfYear + 1) + "."
						+ String.valueOf(dayOfMonth);
				prefBasicAnni.setText(setDt);
				fnSetAnniValue(setDt);
				prefMgr.setPrefAnniversary(setDt);
				fnUpdatePreview();
			}
		};
		Date d = prefMgr.getPrefAnniversary();
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		int mYear = c.get(Calendar.YEAR);
		int mMonth = c.get(Calendar.MONTH);
		int mDay = c.get(Calendar.DATE);
		DatePickerDialog dpd = new DatePickerDialog(this, myDateSetListener,
				mYear, mMonth, mDay);
		dpd.setTitle(R.string.title_anniversary);
		dpd.show();
	}

	private void fnUpdatePreview() {
		fnResetPreviewLayout();

		String tText = prefMgr.getPrefTText();
		String bText = prefMgr.getPrefBText();

		String txt = helper.fnGetDayInStringPattern();
		Bitmap bm = helper.buildBitmapUpdate(txt, false);
		int color = prefMgr.getPrefCounterBG();
		int acolorBG = helper.fnGetAlphaBGColor(color);

		ImageView imgView = (ImageView) prefPreview
				.findViewById(R.id.counter_img);
		TextView topView = (TextView) prefPreview.findViewById(R.id.top_text);
		TextView bottomView = (TextView) prefPreview
				.findViewById(R.id.bottom_text);

		imgView.setImageBitmap(bm);
		imgView.setBackgroundColor(acolorBG);

		topView.setText(tText);
		int topBGColor = prefMgr.getPrefTtextBG();
		int aAopBGColor = helper.fnGetAlphaBGColor(topBGColor);
		int topSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				prefMgr.getPrefTtextSize(), ctx.getResources().getDisplayMetrics());
		topView.setBackgroundColor(aAopBGColor);
		topView.setTextColor(prefMgr.getPrefTtextFontBG());
		topView.setTextSize(topSize);

		bottomView.setText(bText);
		int bottomBGColor = prefMgr.getPrefBtextBG();
		int aBottomBGColor = helper.fnGetAlphaBGColor(bottomBGColor);
		int bottomSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				prefMgr.getPrefBtextSize(), ctx.getResources().getDisplayMetrics());
		bottomView.setBackgroundColor(aBottomBGColor);
		bottomView.setTextColor(prefMgr.getPrefBtextFontBG());
		bottomView.setTextSize(bottomSize);
	}

	private void fnSendUpdateToWidget() {
		String eventName = this.getResources().getString(
				R.string.widget_event_name);
		Intent updateIntent = new Intent(this, WeddingWidgetProvider.class);
		updateIntent.setAction(eventName);
		sendBroadcast(updateIntent);

		RemoteViews remoteViews = new RemoteViews(this.getPackageName(),
				R.layout.widget_layout_1);

		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
		appWidgetManager.updateAppWidget(new ComponentName(this,
				WeddingWidgetProvider.class), remoteViews);
	}

	// OnGestureListener
	@Override
	public boolean onDown(MotionEvent arg0) {
		// Log.d(TAG, "---onDown--- ");
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// Log.d(TAG, "---onFling--- " + e1.toString() + e2.toString());
		ViewFlipper vf = (ViewFlipper) findViewById(R.id.pref_flipper);
		try {
			if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
				return false;
			}
			if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				// right to left swipe

				vf.setInAnimation(AnimationUtils.loadAnimation(
						getApplicationContext(), R.anim.slide_in_right));
				vf.showNext();
				// vf.setOutAnimation(AnimationUtils.loadAnimation(
				// getApplicationContext(), R.anim.slide_out_left));
				// Log.d(TAG, "---onFling--- RIGHT TO LEFT");

			} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				// left to right flip
				vf.setInAnimation(AnimationUtils.loadAnimation(
						getApplicationContext(), android.R.anim.slide_in_left));
				vf.showPrevious();
				// vf.setOutAnimation(AnimationUtils.loadAnimation(
				// getApplicationContext(), android.R.anim.slide_out_right));
				// Log.d(TAG, "---onFling--- LEFT TO RIGHT");

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	// OnGestureListener
	@Override
	public void onLongPress(MotionEvent arg0) {
		// Log.d(TAG, "---onLongPress--- ");
	}

	// OnGestureListener
	@Override
	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// Log.d(TAG, "---onScroll--- ");
		return false;
	}

	// OnGestureListener
	@Override
	public void onShowPress(MotionEvent arg0) {
		// Log.d(TAG, "---onShowPress--- ");
	}

	// OnGestureListener
	@Override
	public boolean onSingleTapUp(MotionEvent arg0) {
		// Log.d(TAG, "---onSingleTapUp--- ");
		return false;
	}

	// OnClickListener
	@Override
	public void onClick(View v) {
		// Log.d(TAG, "Clicked : [" + v.getId() + "]");
		// Page 1
		if (v.getId() == R.id.btn_basic_anni) {
			fnBtnBasicAnniversaryClicked();
		} else if (v.getId() == R.id.btn_basic_counter_type) {
			fnBtnBasicCounterTypeClicked();
		} else if (v.getId() == R.id.btn_basic_layout) {
			fnBtnBasicLayoutClicked();
		} else if (v.getId() == R.id.btn_basic_trans) {
			fnBtnLayoutTransClicked();
		}
		// Page 2
		else if (v.getId() == R.id.btn_counter_bg) {
			fnBtnCounterBgClicked();
		} else if (v.getId() == R.id.btn_counter_font_color) {
			fnBtnCounterFontBgClicked();
		} else if (v.getId() == R.id.btn_counter_pattern) {
			fnBtnCounterPatternClicked();
		} else if (v.getId() == R.id.btn_counter_theme) {
			fnBtnCounterThemeClicked();
		}
		// Page 3
		else if (v.getId() == R.id.btn_ttext_bg) {
			fnBtnTtextBGClicked();
		} else if (v.getId() == R.id.btn_ttext_color) {
			fnBtnTtextFontColorClicked();
		} else if (v.getId() == R.id.btn_ttext_size) {
			fnBtnTtextSizeClicked();
		} else if (v.getId() == R.id.btn_ttext) {
			fnBtnTtextClicked();
		}
		// Page 4
		else if (v.getId() == R.id.btn_btext_bg) {
			fnBtnBtextBGClicked();
		} else if (v.getId() == R.id.btn_btext_color) {
			fnBtnBtextFontColorClicked();
		} else if (v.getId() == R.id.btn_btext_size) {
			fnBtnBtextSizeClicked();
		} else if (v.getId() == R.id.btn_btext) {
			fnBtnBtextClicked();
		}
	}

	private void fnBtnBasicLayoutClicked() {
		OnImgListener imgL = new OnImgListener() {
			@Override
			public void imgValue(int value) {
				Toast.makeText(ctx,
						"New widget layout will be released soon...",
						Toast.LENGTH_LONG).show();
			}
		};
		ImageListDialog imgD = new ImageListDialog(this, imgL,
				R.array.img_array_layout);
		imgD.setTitle(getString(R.string.title_basic_layout));
		imgD.show();

	}

	private void fnBtnBtextSizeClicked() {
		int size = prefMgr.getPrefBtextSize();
		OnSeekListener mlistener = new OnSeekListener() {
			@Override
			public void seekValue(int textSize) {
				prefMgr.setPrefBtextSize(textSize);
				prefBtextSize.setText(textSize + " sp");
				fnUpdatePreview();
			}

			public void seekProgress(int progress) {
				prefBtextSize.setText(progress + " sp");
			}
		};
		SeekBarDialog sd = new SeekBarDialog(this, mlistener, size,
				MAX_FONT_SIZE);
		sd.setTitle(R.string.title_btext_size);
		sd.show();
	}

	private void fnBtnBtextFontColorClicked() {
		int color = prefMgr.getPrefBtextFontBG();
		OnColorPickedListener callback = new OnColorPickedListener() {
			@Override
			public void colorPicked(int color) {
				prefMgr.setPrefBtextFontBG(color);
				fnSetPrefViewBgColor(prefBtextColor, color);
				fnUpdatePreview();
			}
		};
		Dialog cpd = new ColorPickerDialog(this, callback, color, 255);
		cpd.setTitle(R.string.title_btext_color);
		cpd.show();
	}

	private void fnBtnBtextBGClicked() {
		int color = prefMgr.getPrefBtextBG();
		OnColorPickedListener callback = new OnColorPickedListener() {
			@Override
			public void colorPicked(int color) {
				prefMgr.setPrefBtextBG(color);
				fnSetPrefViewBgColor(prefBtextBG, color);
				fnUpdatePreview();
			}
		};
		Dialog cpd = new ColorPickerDialog(this, callback, color, 255);
		cpd.setTitle(R.string.title_btext_bg);
		cpd.show();
	}

	private void fnBtnTtextSizeClicked() {
		int size = prefMgr.getPrefTtextSize();
		OnSeekListener mlistener = new OnSeekListener() {
			@Override
			public void seekValue(int textSize) {
				prefMgr.setPrefTtextSize(textSize);
				prefTtextSize.setText(textSize + " sp");
				fnUpdatePreview();
			}

			public void seekProgress(int progress) {
				prefTtextSize.setText(progress + " sp");
			}
		};
		SeekBarDialog sd = new SeekBarDialog(this, mlistener, size,
				MAX_FONT_SIZE);
		sd.setTitle(R.string.title_ttext_size);
		sd.show();
	}

	private void fnBtnTtextFontColorClicked() {
		int color = prefMgr.getPrefTtextFontBG();
		OnColorPickedListener callback = new OnColorPickedListener() {
			@Override
			public void colorPicked(int color) {
				prefMgr.setPrefTtextFontBG(color);
				fnSetPrefViewBgColor(prefTtextColor, color);
				fnUpdatePreview();
			}
		};
		Dialog cpd = new ColorPickerDialog(this, callback, color, 255);
		cpd.setTitle(R.string.title_ttext_color);
		cpd.show();
	}

	private void fnBtnTtextBGClicked() {
		int color = prefMgr.getPrefTtextBG();
		OnColorPickedListener callback = new OnColorPickedListener() {
			@Override
			public void colorPicked(int color) {
				prefMgr.setPrefTtextBG(color);
				fnSetPrefViewBgColor(prefTtextBG, color);
				fnUpdatePreview();
			}
		};
		Dialog cpd = new ColorPickerDialog(this, callback, color, 255);
		cpd.setTitle(R.string.title_ttext_bg);
		cpd.show();
	}

	private void fnBtnCounterFontBgClicked() {
		int color = prefMgr.getPrefCounterFontBG();
		OnColorPickedListener callback = new OnColorPickedListener() {
			@Override
			public void colorPicked(int color) {
				prefMgr.setPrefCounterFontBG(color);
				fnSetPrefViewBgColor(prefCounterFontColor, color);
				fnUpdatePreview();
			}
		};

		Dialog cpd = new ColorPickerDialog(this, callback, color, 255);
		cpd.setTitle(R.string.title_counter_font_bg);
		cpd.show();
	}

	/*
	 * Activity Handler onResume onRestart onPause onStop onDestroy
	 */
	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
		finish();
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "onStop");
		fnSendUpdateToWidget();
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
		fnUpdateActivity();
	}

	@Override
	public void onRestart() {
		super.onRestart();
		Log.d(TAG, "onRestart");
	}
}
